package de.velvettear.squirrel.adapter;

import java.util.Collections;
import java.util.Comparator;
import java.util.Vector;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import de.velvettear.squirrel.R;
import de.velvettear.squirrel.kernel.NFCTag;
import de.velvettear.squirrel.kernel.Statics;

public class NFCTagAdapter extends BaseExpandableListAdapter {

	private Context context;
	
	private Vector<NFCTag> groups;
		
	public NFCTagAdapter(Context context, Vector<NFCTag> groups) {
		this.context = context;
		this.groups = groups;
		sort();
	}
	
	public void addItem(NFCTag tag) {
		groups.add(tag);
		sort();
		refresh();
	}

	public NFCTag getItem(int pos) {
		return groups.elementAt(pos);
	}
	
	public void removeItem(int pos) {
		groups.remove(pos);
		sort();
		refresh();
	}
	
	public Object getChild(int groupPosition, int childPosition) {
		return groups.get(groupPosition);
	}

	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
		ChildrenViewHolder childrenViewHolder;
		NFCTag tag = groups.elementAt(groupPosition);
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.expandable_child_nfctags, null);
			
			childrenViewHolder = new ChildrenViewHolder();
			childrenViewHolder.tv_action = (TextView) convertView.findViewById(R.id.expandable_child_nfctags_tv_action);
			childrenViewHolder.ll_extras = (LinearLayout) convertView.findViewById(R.id.expandable_child_nfctags_ll_extras);
			childrenViewHolder.iv_wlan = (ImageView) convertView.findViewById(R.id.expandable_child_nfctags_iv_wlan);
			childrenViewHolder.iv_bluetooth = (ImageView) convertView.findViewById(R.id.expandable_child_nfctags_iv_bluetooth);
			
			convertView.setTag(childrenViewHolder);
		} else {
			childrenViewHolder = (ChildrenViewHolder) convertView.getTag();
		}
		
		childrenViewHolder.tv_action.setText(tag.m_sAction);
		
		if (tag.hasExtras()) {
			childrenViewHolder.ll_extras.setVisibility(View.VISIBLE);
		} else {
			childrenViewHolder.ll_extras.setVisibility(View.GONE);
		}
		
		if (tag.hasExtra(Statics.NFC_EXTRA_WLAN)) {
			childrenViewHolder.iv_wlan.setVisibility(View.VISIBLE);
		} else {
			childrenViewHolder.iv_wlan.setVisibility(View.GONE);
		}
		
		if (tag.hasExtra(Statics.NFC_EXTRA_BLUETOOTH)) {
			childrenViewHolder.iv_bluetooth.setVisibility(View.VISIBLE);
		} else {
			childrenViewHolder.iv_bluetooth.setVisibility(View.GONE);
		}
				
		return convertView;
	}

	public int getChildrenCount(int groupPosition) {
		return 1;
	}

	public NFCTag getGroup(int groupPosition) {
		return groups.get(groupPosition);
	}

	public int getGroupCount() {
		return groups.size();
	}

	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		GroupViewHolder groupViewHolder;
		NFCTag tag = groups.elementAt(groupPosition);
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.expandable_group_nfctags, null);
			
			groupViewHolder = new GroupViewHolder();
			groupViewHolder.tv_name = (TextView) convertView.findViewById(R.id.expandable_group_nfctags_tv_action);
			groupViewHolder.iv_type = (ImageView) convertView.findViewById(R.id.expandable_group_nfctags_iv_action);
			groupViewHolder.v_separator = (View) convertView.findViewById(R.id.expandable_group_nfctags_separator);
			
			convertView.setTag(groupViewHolder);
		} else {
			groupViewHolder = (GroupViewHolder) convertView.getTag();
		}
		
		groupViewHolder.tv_name.setText(tag.m_sName);
		
		if (tag.m_sType.equals(Statics.NFC_TYPE_URL)) {
			groupViewHolder.iv_type.setImageResource(R.drawable.icon_web);
		} else if (tag.m_sType.equals(Statics.NFC_TYPE_APP)) {
			groupViewHolder.iv_type.setImageResource(R.drawable.icon_externalapp);
		}
					
		if (isExpanded) {
			groupViewHolder.v_separator.setVisibility(View.GONE);
		} else {
			groupViewHolder.v_separator.setVisibility(View.VISIBLE);
		}
		
		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return false;
	}
	
	public void refresh() {
		this.notifyDataSetChanged();
	}
	
	public void sort() {
		Collections.sort(groups, new Comparator<NFCTag>() {			
			@Override
			public int compare(NFCTag tag1, NFCTag tag2) {
				int comparison = tag1.m_sName.compareToIgnoreCase(tag2.m_sName);
				return 0;
			}
		});		
	}
	
	static class GroupViewHolder {
		TextView tv_name;
		ImageView iv_type;
		View v_separator;
	}
	
	static class ChildrenViewHolder {
		TextView tv_action;
		LinearLayout ll_extras;
		ImageView iv_wlan;
		ImageView iv_bluetooth;
	}
	
}
